<template>
  <div class="tab-wrap">
    <div :class="['title', 'tab']">
      {{ title }}
    </div>
    <div
      :class="[{ active: value.length === options.length }, 'tab']"
      @click="handleAll"
    >
      全部
    </div>
    <div
      :class="[{ active: value.includes(item.value) }, 'tab']"
      v-for="item in options"
      :key="item.value"
      @click="handleClick(item)"
    >
      {{ item.label }}
    </div>
  </div>
</template>

<script>
export default {
  props: {
    options: {
      type: Array,
      default: () => [],
    },
    value: {
      type: Array,
      default: () => [],
    },
    title: {
      type: String,
      default: "",
    },
  },
  data() {
    return {};
  },
  components: {},
  computed: {},
  created() {},
  mounted() {},
  watch: {
    value: {
      handler() {},
      immediate: true,
    },
  },
  methods: {
    handleClick({ value }) {
      let arr = [...this.value];
      if (arr.includes(value)) {
        arr = arr.filter((item) => item !== value);
      } else {
        arr.push(value);
      }
      this.$emit("input", arr);
      this.$emit("change", arr);
    },
    handleAll() {
      if (this.value.length === this.options.length) {
        this.$emit("input", []);
      } else {
        this.$emit(
          "input",
          this.options.map((item) => item.value)
        );
      }
    },
  },
};
</script>

<style scoped>
.tab-wrap {
  display: flex;
  align-items: center;
  position: relative;
  box-sizing: border-box;
  margin-bottom: 15px;
  margin-top: -30px;
}
.tab {
  text-align: center;
  height: 50px;
  line-height: 50px;
  color: #303133;
  box-sizing: border-box;
  margin: 0 5px;
  padding: 0 10px;
  cursor: pointer;
  position: relative;
  z-index: 10;
  transition: all 0.5s;
}
.tab:not(.title):hover {
  color: ;
}
.tab.active {
  color: #fff;
  background-color: #409eff;
}
.title {
  font-weight: bold;
  margin: 0;
  padding: 0;
}
</style>
